package com.baosight.sczx.common.zm.basetools.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBase;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBaseHisDetail;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCM;

/**
 * 
 * <p>Title: AutoMaterialBaseHis.java</p>
 * <p>Description: 库存管理自动结转</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author bobswang
 * @version 1.0
 * <pre>Histroy:
 *          2011-9-15 下午04:09:47  高强  Create
 *</pre>
 */
public class AutoMaterialBaseHis {
	private Dao dao;
	
	public AutoMaterialBaseHis(){
	}
	
	public void setDao(Dao dao)
	{
		this.dao = dao;
	}
	
	/**
	 * 根据系统设置生成结转记录
	 * <p>Function: GenerateDuty</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-5上午09:11:38 严洪浩
	 * </p>
	 *
	 */
	public void GenerateMaterialBaseHis() throws Exception {
		String auto= SystemSettingCM.getInstance().getIsAutoBase();//是否自动结转
		long autoDate= SystemSettingCM.getInstance().getAutoBaseDate();//获得自动结转日期（月日）
		String currentDate = StaticTools.getDate();//当前日期
		String year = StaticTools.getYear();//当前年
		int moth = Integer.parseInt(StaticTools.getDateforDay().substring(0, 2));//当前月
		int date = Integer.parseInt(StaticTools.getDateforDay().substring(2));//当前日
		
		//如果系统设置为不自动结转
		if(auto.equals("0"))
		{
			return;
		}
        //如果系统设置结转日期不为今日
		if(date != autoDate)
		{
			return;
		}
		
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdHisDate", currentDate);
		
		List<?>  schedule_result = dao.query("ZMCMMaterialBaseHis.query", _argMap);
		//如果有结转日期的记录，则不执行
		if(schedule_result.size() != 0)
		{
			return;
		}
		_argMap = new HashMap<String, String>();		
		_argMap.put("fdYear", year);
		_argMap.put("fdMonth", moth+"");
		boolean isRepeat = validate("ZMCMMaterialBaseHis.validate", _argMap);
        //如果当前年月有结转记录，则不执行
		if(isRepeat) {
			return;
		}
		else {
			
			Map<String, String> _insertMap = new HashMap<String, String>();
			_insertMap.put("fdKeyId", TablePk.getInstance().getNextValue("T_CM_MATERIALBASEHIS"));
			_insertMap.put("fdPeople", UserSession.getUserId().toString());
			_insertMap.put("fdYear", year);
			_insertMap.put("fdMonth", moth+"");
			_insertMap.put("fdHisDate", currentDate);
			dao.insert("ZMCMMaterialBaseHis.insert", _insertMap);
			
//			结转时修改库存所有数据的结转年月
			boolean updateYearMonthForHis = MaterialBase.UpdateYearMonthForHis(year,moth+"");
			if(!updateYearMonthForHis) {
				System.out.println("库存数据的结转失败！");
				return ;
			}else{
				//结转时修改库存所有数据的结转年月
				boolean insertHisDetailFromBase = MaterialBaseHisDetail.InsertHisDetailFromBase();
				if(!insertHisDetailFromBase) {
					System.out.println("生成结转数据失败！");
					return ;
				}
			}
		}
	}
	
	/**
	 * 结转年月重复验证
	 * <p>Function: validateNo</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:13:00 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			System.out.println("提交失败，结转年份、结转月份重复！");
			return true;
		}
		return false;
	}
}
